import { createApp } from "vue";
import App from "./App.vue";
import router from "./router/index.js"; // 引入路由模块
import axiosInstance from "./request/axios.js";
import store from "./store/index.js";
import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import * as ElementPlusIconsVue from '@element-plus/icons-vue';

const app = createApp(App);

// 添加路由守卫
router.beforeEach((to, from, next) => {
  const isLoggedIn = store.state.isLoggedIn; // 假设在Vuex的state中存储了登录状态，名为isLoggedIn，你可根据实际情况调整
  if (to.name !== "loginPage" && !isLoggedIn) {
    // 如果要访问的不是登录页且未登录，就跳转到登录页
    next({ name: "loginPage" });
  } else {
    next();
  }
});

app.use(router); // 挂载路由
app.config.globalProperties.$axios = axiosInstance;
app.use(ElementPlus);
// 注册所有图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.use(store);
app.mount("#app");
